#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include "u1.h"

/* Read adjacency list (see u1.ForwardLinks) and write out an equivalent sparse
 * matrix in the format
 * source target 1
 * target source 1
 * ...
 * for every link from source to target.
 *
 * Note that self links are not removed. Note that there may be duplicate
 * entries; you can get rid of these with the sort & uniq shell commands, if you
 * want to.
 *
 * You can load this file into a sparse matrix in Matlab using
 * A = spconvert(load('...'));
 */

using namespace std;

int main(int argc, char* argv[]) {
  TRACE << "Reading pages from stdin..." << endl;
  int page_count = 0, total_link_count = 0;
  while (cin.good()) {
    int page_number, link_count, link;
    cin >> page_number >> link_count;
    if (!cin.good()) break;

    if (page_number < 0) {
      TRACE << "Negative page numbers not allowed (read " << page_count
        << " pages before error)." << endl;
      exit(EXIT_FAILURE);
    }

    if (link_count < 0) {
      TRACE << "Negative link count not allowed (read " << page_count
        << " pages before error)." << endl;
      exit(EXIT_FAILURE);
    }

    for (int i = 0; i < link_count; ++i) {
      cin >> link;
      if (!cin.good()) {
        TRACE << "Unexpected end of file on " << i << "th of " << link_count
          << " links when reading " << page_count << "th page)." << endl;
        exit(EXIT_FAILURE);
      }

      if (link < 0) {
        TRACE << "Negative link not allowed (read " << page_count
          << " pages before error)." << endl;
        exit(EXIT_FAILURE);
      }

      cout<<page_number<<" "<<link<<" 1"<<endl;
      cout<<link<<" "<<page_number<<" 1"<<endl;
    }

    // For giving better error messages.
    total_link_count += link_count;
    ++page_count;
  }
  TRACE << "Read " << page_count << " pages and " << total_link_count
    << " links from stdin." << endl;
}
/*
* Copyright (c) 2009 John Lees-Miller
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

